package com.zhp.dao;

import com.zhp.common.Constant;
import com.zhp.dao.base.BaseDao;
import com.zhp.model.ConJoin;
import com.zhp.model.Conference;
import com.zhp.utils.JDBCUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MeetDao extends BaseDao<Conference> {
    public MeetDao() {
        super(Conference.class);
    }

    public List<Conference> findConferences(String deptName, int currentPage, String name) {
        String sql = "";
        if ("".equals(deptName) || deptName == null) {
            sql = "select * from conference LIMIT ?,?";
            if (name != null) {
                sql = "select * from conference where title like ? LIMIT ?,?";
            }
        } else {
            sql = "select * from conference where dept_name = ? LIMIT ?,?";
            if (name != null) {
                sql = "select * from conference where dept_name = ? and title like ? LIMIT ?,?";
            }
        }
        List<Conference> list = new ArrayList<>();
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        BasicRowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
        try {
            if ("".equals(deptName) || deptName == null) {
                if (null == name) {
                    list = queryRunner.query(sql, new BeanListHandler<>(Conference.class, processor), (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
                } else {
                    list = queryRunner.query(sql, new BeanListHandler<>(Conference.class, processor), "%" + name + "%", (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
                }
            } else {
                if (null == name) {
                    list = queryRunner.query(sql, new BeanListHandler<>(Conference.class, processor), "", (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
                } else {
                    list = queryRunner.query(sql, new BeanListHandler<>(Conference.class, processor), deptName, "%" + name + "%", (currentPage - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public long getConferenceTotal(String name) {
        long total = 0L;
        String sql = "select count(*) as total from conference  ";
        if (null != name) {
            sql = "select count(*) as total from conference where dept_name like ? ";
        }
        Map<String, Object> map = new HashMap<>();
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null == name) {
                map = queryRunner.query(sql, new MapHandler());
            } else {
                map = queryRunner.query(sql, new MapHandler(), "%" + name + "%");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (map != null) {
            total = Long.parseLong(map.get("total").toString());
        }
        return total;

    }

    public Object findInfo(String mId) {
        String sql = "SELECT count(*)\n" +
                "from user u,conference cf\n" +
                "where cf.id = ? and EXISTS(select 1 from con_join cj where cf.id = cj.c_id and cj.u_id =u.id)";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            return queryRunner.query(sql, new ScalarHandler<>(), mId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean endMeet(String mId) {
        String sql = "UPDATE conference SET status = 2 WHERE id = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            int row = queryRunner.update(sql, mId);
            return row > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }
}
